//
//  AdvertisingCell.swift
//  iModern
//
//  Created by Ralf on 6/9/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit

class AdvertisingCell: ArticleCell
{
    let __summary_label_tag = 24

    let __buy_button_tag = 26

    let __service_description_background_image_view = 14

    let __lab = 70
    
    var buttonData = NSMutableDictionary()

    override func awakeFromNib()
    {
//        super.awakeFromNib()
        initAvatarImageView()
        initAliasLabel()
        initPreviewImageView()

        initServiceDescriptionBackgroundImageView()
        initSummaryLabel()
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    func setSummaryLabel(summary: String)
    {
        let label = self.viewWithTag(__summary_label_tag) as! UILabel
        label.text = summary
    }

    override func setMessageTextView(text: String)
    {
        var textView = self.viewWithTag(__message_textview_tag) as? UITextView
        if textView != nil
        {
            textView!.removeFromSuperview()
            textView = nil
        }
        let x      = 0
        let y      = 60 + screen.size.width
        let height = self.bounds.height - y - 35
        let frame  = CGRect(x: CGFloat(x), y: y, width: screen.width, height: height)
        textView = UITextView(frame: frame)
        textView!.tag = __message_textview_tag
        textView!.scrollEnabled = false
        textView!.userInteractionEnabled = false
        self.addSubview(textView!)

        let paragraphStyle = NSMutableParagraphStyle()

        paragraphStyle.lineHeightMultiple = 16
        paragraphStyle.maximumLineHeight = 20
        paragraphStyle.minimumLineHeight = 20

        paragraphStyle.alignment = .Left

        let attributes = [NSFontAttributeName: UIFont.systemFontOfSize(12),
                          NSParagraphStyleAttributeName: paragraphStyle,
                          NSForegroundColorAttributeName: UIColor.darkGrayColor()]
        textView!.attributedText = NSAttributedString(string: text, attributes: attributes)
    }

    func initSummaryLabel()
    {
        var label = self.viewWithTag(__summary_label_tag) as? UILabel
        if label == nil
        {
            //let y     = screen.width
            let frame = CGRect(x: 10, y: screen.width + 20, width: screen.width - 120, height: 40)
            label = UILabel(frame: frame)
            label!.font = UIFont.systemFontOfSize(14)
            label!.textAlignment = .Left
            label!.text = "服务"
            label!.tag = __summary_label_tag
            self.addSubview(label!)
        }
    }

    func initServiceDescriptionBackgroundImageView()
    {
        var imageView = self.viewWithTag(__service_description_background_image_view) as? UIImageView
        if imageView == nil
        {
            let frame = CGRect(x: 0, y: screen.width + 20, width: screen.width, height: 40)
            imageView = UIImageView(frame: frame)
//            imageView!.image = UIImage(named: "ServiceDescriptionBackground.png")
            imageView!.backgroundColor = UIColor.whiteColor()
            imageView!.tag = __service_description_background_image_view
            imageView!.alpha = 0.8
            self.addSubview(imageView!)
        }
    }

    override func setNumberOfFavoritesButton(title: String)
    {
        var button = self.viewWithTag(__number_of_favorites_button_tag) as? UIButton

        if button != nil
        {
            button!.removeFromSuperview()
            button = nil
        }

        let y     = bounds.height - 35
        let frame = CGRect(x: 5, y: y, width: 80, height: 30)
        button = UIButton(frame: frame)
        button!.setTitle(title, forState: .Normal)
        button!.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button!.titleLabel!.font = UIFont.systemFontOfSize(12)
        button!.imageView?.contentMode = .ScaleAspectFit
        button!.setImage(UIImage(named: "Favorites-Small.png"), forState: .Normal)
        button!.imageEdgeInsets = UIEdgeInsetsMake(5, -5, 5, 20)
        button!.layer.borderWidth = 1.0
        button!.layer.borderColor = UIColor.lightGrayColor().CGColor
        button!.layer.masksToBounds = true
        button!.layer.cornerRadius = 4
        button!.tag = __number_of_favorites_button_tag

        self.addSubview(button!)
    }

    func setBuyButton(price: Int, buttonData: NSMutableDictionary)
    {
        self.buttonData = buttonData
        var button = self.viewWithTag(__buy_button_tag) as? UIButton

        if button != nil
        {
            button!.removeFromSuperview()
            button = nil
        }

        let y     = bounds.height - 35
        let x     = bounds.width - 145
        let frame = CGRect(x: x, y: y, width: 140, height: 30)
        button = UIButton(frame: frame)
        button!.setTitle("立即下单 ¥\(price).00", forState: .Normal)
        button!.titleLabel?.font = UIFont.systemFontOfSize(12)
        button!.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button!.backgroundColor = UIColor(red: 0.40784313725490196, green: 0.6588235294117647, blue: 0.19215686274509805, alpha: 1)
        button!.layer.masksToBounds = true
        button!.layer.cornerRadius = 4
        button!.tag = __buy_button_tag
        button!.addTarget(self, action: Selector("buy"), forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(button!)

    }
    


    func buy()
    {
//        if appDelegate.isLogged()
//        {
//            let controller = MakeAppointmentFormViewController()
//            controller.advertising = buttonData
//            appDelegate.navigationController?.pushViewController(controller, animated: true)
//        }
//        else
//        {
//            self.login()
//        }
        let article = buttonData["article"] as! NSDictionary
        let status = article["status"] as? String
        if status == "Ready for Sale"
        {
            if appDelegate.isLogged()
            {
                let controller = MakeAppointmentFormViewController()
                controller.advertising = buttonData
                appDelegate.navigationController?.pushViewController(controller, animated: true)
            }
            else
            {
                self.login()
            }
        }
        else
        {
            self.alertWarningMessage("该商品已下架")
            return
        }
    }
}
